//
//  LHXWritingViewController.m
//  Etwo
//
//  Created by 林浩翔 on 16/6/19.
//  Copyright © 2016年 林浩翔. All rights reserved.
//

#import "LHXWritingViewController.h"
#import "LHXCutImageView.h"
#import "LHXBtnMenusViewController.h"
#import "LHXMainBtnItem.h"
#import "LHXBtnItem.h"

@interface LHXWritingViewController ()


@end

@implementation LHXWritingViewController

#pragma mark --懒加载
- (NSArray *)btnItemArray{
    if (_btnItemArray == nil) {
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"MainBtn" ofType:@"plist"];
        NSArray *BtnItemArray = [NSArray arrayWithContentsOfFile:plistPath];
        LHXMainBtnItem *MainItem = [LHXMainBtnItem MainItemWithArray:BtnItemArray];
        NSMutableArray *tempArray = [NSMutableArray array];
        for (NSDictionary *dict in MainItem.EditFontViewBtnItem) { // 遍历字典数组中的每一个字典
            // 将字典 转为 模型
            LHXBtnItem *btnItem = [LHXBtnItem itemWithDict:dict];
            
            [tempArray addObject:btnItem];
        }
        _btnItemArray = tempArray;
        
    }
    return _btnItemArray;
}


#pragma mark --view生命周期
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self addPan];

}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.cutImageView.frame = self.cutImageView.fristFrame;
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
}




#pragma mark --按钮点击
- (void)BtnClick:(LHXbutton *)btn{
    [LHXchoose shareChoose].cutImageView = self.cutImageView;
    [LHXchoose shareChoose].chooseImage = self.cutImageView.image;
    
    if ([btn.btnTextTag isEqualToString:@"菜单"]) {
        LHXBtnMenusViewController *menusVC = [[LHXBtnMenusViewController alloc]init];
        
        menusVC.disenbleBtnName = @"文字工具";
        
        menusVC.transitioningDelegate = self;
        
        menusVC.modalPresentationStyle = UIModalPresentationCustom;
        
        [self presentViewController:menusVC animated:YES completion:nil];
    }else if([btn.btnTextTag isEqualToString:@"重新选图"]){
        [self getPicture];
    }else if([btn.btnTextTag isEqualToString:@"保存"]){
        [self creadAlertWithTitle:nil andMessage:@"确定要保存图片吗?"];
    }else if([btn.btnTextTag isEqualToString:@"文字工具"]){
//        self.btnScrollView.hidden = YES;
//        
//        [self.btnScrollView removeFromSuperview];
//        //创建label添加在图片上
//        [self editFontForImage];
//        
    }
}


/**文字功能(未实现)*/
- (void)editFontForImage{
    UITextField *editTextField = [[UITextField alloc]init];
    
    editTextField.frame = CGRectMake(self.cutImageView.frame.size.width * 0.2, self.cutImageView.frame.size.height - 20, 200, 100);
    
    editTextField.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];
    
    [self.view addSubview:editTextField];
}



/**添加滑动手势*/
- (void)addPan{
    
    if(self.cutImageView.pan == nil){
        //创建手势
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.cutImageView action:@selector(pan:)];
        
        self.cutImageView.pan = pan;
        //添加手势
        [self.cutImageView addGestureRecognizer:pan];
    }
    
    
}

#pragma mark --创建弹窗(点确定保存到系统相册)
- (void)creadAlertWithTitle:(NSString *)title andMessage:(NSString *)message{
    //创建弹窗
    UIAlertController *AlertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *SaveAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImageWriteToSavedPhotosAlbum(self.cutImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }];
    
    UIAlertAction *CancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LHXeditImage" object:self];
    }];
    
    [AlertController addAction:CancelAction];
    
    [AlertController addAction:SaveAction];
    
    
    [self presentViewController:AlertController animated:YES completion:nil];
}


/**保存图片后执行的操作*/
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    
    NSLog(@"保存成功");
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
